package com.radiation.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.radiation.rms.common.R;
import com.radiation.rms.common.RMSCommon;
import com.radiation.rms.common.RMSConstant;
import com.radiation.rms.entity.Area;
import com.radiation.rms.mapper.AreaMapper;
import com.radiation.rms.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.radiation.rms.vo.AddressMapVo;
import com.radiation.rms.vo.ConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 地点表 服务实现类
 * </p>
 *
 * @author 1062
 * @since 2021-04-02
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private RMSCommon rmsCommon;

    @Override
    public void batchSave(List<AddressMapVo> addressMapVo, String parentId) {
        for (AddressMapVo mapVo : addressMapVo) {
            if (mapVo.getDistricts() != null) {
                batchSave(mapVo.getDistricts(), mapVo.getAdcode());
            }
            Area area = new Area();
            area.setName(mapVo.getName());
            area.setLevel(mapVo.getLevel());
            String center = mapVo.getCenter();
            String[] split = center.split(",");
            area.setLongitude(Double.parseDouble(split[0]));
            area.setLatitude(Double.parseDouble(split[1]));
            area.setFatherId(parentId);
            this.baseMapper.insert(area);
        }
    }

    @Override
    public R getAreaTree() {
        List<Area> areas = areaMapper.selectList(null);
        List<Area> root = new ArrayList<>();
        Map<String, Area> map = areas.stream().collect(Collectors.toMap(Area::getId, area -> area));
        for (Area area : areas) {
            if ("0".equals(area.getFatherId())) {
                root.add(area);
                continue;
            }
            map.get(area.getFatherId()).getChild().add(area);
        }
        return R.ok().data("items", root);
    }

    @Override
    public R getInitArea() {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("father_id", "320900").select("id,name");
        List<Area> areas = this.baseMapper.selectList(queryWrapper);
        return R.ok().data("items", areas);
    }

    @Override
    public String getPath(String areaId) {
        return areaMapper.getPath(areaId);
    }

    @Override
    public R saveArea(Area area) {
        this.baseMapper.insert(area);
        return R.ok();
    }

    @Override
    public R removeBid(String areaId) {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("father_id", areaId);
        Integer count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return R.error().message(RMSConstant.DELETE_ERROR);
        }
        this.baseMapper.deleteById(areaId);
        return R.ok();
    }

    @Override
    public List<String> getOneDepartment(String areaId) {
        return areaMapper.getOneDepartment(areaId);
    }

    @Override
    public List<String> getTwoDepartment(String areaId) {

        return areaMapper.getTwoDepartment(areaId);
    }

    @Override
    public List<String> getThreeDepartment(String areaId) {
        return areaMapper.getThreeDepartment(areaId);
    }

    @Override
    public List<String> getParent(String areaId) {
        List<String> aeaPath = new ArrayList<>();
        aeaPath.add(areaId);
        List<String> longs = recursiveSearch(areaId, aeaPath);
        Collections.reverse(longs);
        return longs;
    }

    @Override
    public List<ConditionVo> getConditionStatus(String hospitalId) {

        return baseMapper.getConditionStatus(hospitalId);
    }

    @Override
    public R initHospital() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<String> departmentIds = rmsCommon.getDepartmentIds(username);
        if (departmentIds==null || departmentIds.size()==0){
            return R.ok().data("items",new ArrayList<Area>());
        }
        List<Area> areas =  this.baseMapper.getHospital(departmentIds);
        return R.ok().data("items",areas);
    }

    @Override
    public List<String> getHospitalId(List<String> departmentIds) {

        return baseMapper.getHospitalId(departmentIds);
    }

    private List<String> recursiveSearch(String catelogId, List<String> cateLogPath) {
        Area area = baseMapper.selectById(catelogId);
        if (area != null && !("0".equals(area.getFatherId()))) {
            cateLogPath.add(area.getFatherId());
            recursiveSearch(area.getFatherId(), cateLogPath);
        } else {
            return cateLogPath;
        }
        return cateLogPath;
    }
}
